﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.controls.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class MetaMenuMediator extends Mediator implements IMediator, ITicking {

		[Inject]
        public var metaMenu:MetaMenu;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var energyModel:EnergyModel;
		[Inject]
        public var sessionModel:SessionModel;
		[Inject]
        public var sunModel:SunModel;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var environmentModel:EnvironmentModel;
		[Inject]
        public var missionModel:MissionModel;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var contractsModel:ContractsModel;
		[Inject]
        public var worldModel:WorldModel;
        protected var _fpsCounter:Label;

        override public function onRegister():void{
            eventMap.mapListener(eventDispatcher, UserModelEvent.ON_MODEL_CHANGED, this.updateDisplay);
            eventMap.mapListener(eventDispatcher, KeyboardInputEvent.ON_CHEAT_MODE, this.onCheatMode);
            eventMap.mapListener(eventDispatcher, EventConstants.INIT_WORLD_COMPLETE, this.onWorldComplete);
            eventMap.mapListener(eventDispatcher, EventConstants.SHOW_MISSIONS, this.onShowMissions);
            eventMap.mapListener(eventDispatcher, EventConstants.VISIT_FRIEND, this.onVisitFriend);
            eventMap.mapListener(eventDispatcher, EventConstants.UPDATED_CONTRACT, this.onUpdateContracts);
            eventMap.mapListener(eventDispatcher, EventConstants.KIOSK_AVAILABLE, this.onKioskAvailable);
            this.metaMenu.energyBar.addEventListener(MouseEvent.CLICK, this.onClickEnergyBar);
            this.metaMenu.goodsDisplay.addEventListener(MouseEvent.CLICK, this.onClickGoodsBar);
            this.metaMenu.setXPTooltipLabel(this.languageModel.getTextByKey("meta_xp_tooltip"));
            this.metaMenu.setGoodsTooltip(this.languageModel.getTextByKey("goods_bar_mouseover"));
            this.metaMenu.setEnergyAddButtonLabel(this.languageModel.getTextByKey("meta_energy_button"));
            this.setTooltipText();
            this.updateDisplay();
            this.onUpdateContracts(null);
            this.checkGoodsbar();
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            this.setTooltipText();
            if (this._fpsCounter){
                this._fpsCounter.setLabel(this.tickModel.currentFPS.toString());
            };
        }
        protected function setTooltipText():void{
            var _local1:String = TimeUtils.secondsToHoursMinSec(this.energyModel.nextRefillIn);
            var _local2:Object = {energy:(this.energyModel.energy + this.energyModel.energyExtra), energy_total:this.energyModel.energyMax, time:_local1};
            var _local3:String = ((((this.energyModel.nextRefillIn <= 0)) && ((this.energyModel.energy == this.energyModel.energyMax)))) ? "meta_energy_tooltip_finish" : "meta_energy_tooltip";
            this.metaMenu.setEnergyTooltip(this.languageModel.getTextByKey(_local3, _local2));
            this.metaMenu.updateEnergy(this.energyModel.energy, this.energyModel.energyExtra, this.energyModel.energyMax);
            this.metaMenu.setMaxEnergyPoints(this.energyModel.energyMax);
        }
        protected function onCheatMode(_arg1:KeyboardInputEvent):void{
            var _local2:String;
            this.showCheatButton("cheatButtonsDisplay");
            for each (_local2 in this.sessionModel.savepoints) {
                this.metaMenu.createCheatButton(_local2);
            };
            this.addTestTextField(this.languageModel.getTextByKey(("testing_characters_" + this.environmentModel.language)));
            this.addDummyButton();
            this.addFPSDisplay();
        }
        protected function addFPSDisplay():void{
            var _local1:UIComponent;
            if (!this._fpsCounter){
                _local1 = UIComponent(this.metaMenu.getElementsByClassName("cheatButtonsDisplay")[0]);
                this._fpsCounter = RepriseUtils.addLabel(_local1, "fpsCounter");
            };
        }
        protected function addTestTextField(_arg1:String):void{
            var _local2:UIComponent = UIComponent(this.metaMenu.getElementsByClassName("cheatButtonsDisplay")[0]);
            var _local3:UIComponent = _local2.addComponent("textTestDisplay");
            var _local4:Label = Label(_local3.addComponent("textField showcardGothicFont", null, Label));
            _local4.setLabel(_arg1);
            var _local5:Label = Label(_local3.addComponent("textField showcardGothicFontOutline", null, Label));
            _local5.setLabel(_arg1);
            var _local6:Label = Label(_local3.addComponent("textField myriadProFont", null, Label));
            _local6.setLabel(_arg1);
            var _local7:Label = Label(_local3.addComponent("textField myriadProFontOutline", null, Label));
            _local7.setLabel(_arg1);
        }
        protected function addDummyButton():void{
            var _local1:UIComponent = UIComponent(this.metaMenu.getElementsByClassName("cheatButtonsDisplay")[0]);
            var _local2:LabelButton = RepriseUtils.addLabelButton(_local1, "httpCodeButton", "Click");
            eventMap.mapListener(_local2, MouseEvent.CLICK, this.onDummyClicked);
        }
        protected function onDummyClicked(_arg1:MouseEvent):void{
            var _local2:InfoEvent = new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.EXTRA_BOOST_POPUP);
            dispatch(_local2);
        }
        protected function showCheatButton(_arg1:String):void{
            var _local2:UIComponent = UIComponent(this.metaMenu.getElementsByClassName(_arg1)[0]);
            ((_local2) && (_local2.show()));
        }
        protected function updateDisplay(_arg1:UserModelEvent=null):void{
            this.metaMenu.updateXP(String(this.userModel.xpPoints), this.userModel.xpBounds[1]);
            var _local2:Number = ((this.userModel.xpPoints - this.userModel.xpBounds[0]) / (this.userModel.xpBounds[1] - this.userModel.xpBounds[0]));
            this.metaMenu.updateBar(_local2);
            this.metaMenu.updateLevel(this.userModel.level);
            this.metaMenu.updateEnergy(this.energyModel.energy, this.energyModel.energyExtra, this.energyModel.energyMax);
            this.metaMenu.updateGoods(this.userModel.goods, GameConstants.MAX_GOODS_ITEM_NUM);
        }
        protected function onShowMissions(_arg1:Event):void{
            this.metaMenu.setMissionTooltip(this.languageModel.getTextByKey((("mission_" + this.missionModel.defaultMission.code) + "_title")));
        }
        protected function onWorldComplete(_arg1:Event):void{
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
        }
        protected function onClickEnergyBar(_arg1:MouseEvent):void{
            dispatch(new Event(EventConstants.OPEN_BOOST_SHOP_POPUP));
        }
        private function onKioskAvailable(_arg1:Event):void{
            this.checkGoodsbar();
        }
        private function onClickGoodsBar(_arg1:MouseEvent):void{
            if (this.contractsModel.state == ContractsModel.READY_TO_CREATE){
                dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.GOODS_POPUP, null));
            };
        }
        private function onUpdateContracts(_arg1:Event):void{
            var _local2 = (this.contractsModel.state == ContractsModel.READY_TO_CREATE);
            this.metaMenu.enableGoodsDisplay(_local2);
        }
        protected function onVisitFriend(_arg1:InfoEvent):void{
            this.checkGoodsbar();
        }
        protected function checkGoodsbar():void{
            var _local1:Boolean = this.worldModel.hasAreaType(GameConstants.KIOSK);
            if (_local1){
                this.metaMenu.goodsDisplay.show();
            };
        }

    }
}//package net.wooga.pets.views 
